package com.minwei.bigrichman.utils;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.security.SecureRandom;
import java.time.Instant;

/**
 * @author: <a href="xjm01611253@alibaba-inc.com">肖建民</a>
 * @date: 2024/8/9
 * @time: 17:48
 * @description:
 */
@Component
public class UniqueRoomIdGenerator {

    private static final SecureRandom random = new SecureRandom();

    public long generateUniqueId() {
        // 获取当前时间的毫秒数
        long currentTimeMillis = Instant.now().toEpochMilli();

        // 生成一个0到9999之间的随机数
        int randomNum = random.nextInt(10000);

        // 将时间戳和随机数结合，确保前4位为时间戳，后4位为随机数，以增加唯一性
        // 注意：此方法在高并发下可能产生冲突
        long uniqueId = (currentTimeMillis % 10000) * 10000 + randomNum;

        return uniqueId;
    }
}
